package org.hq.upvideo;

import com.alibaba.fastjson2.JSON;
import com.hq.utils.jsoup.SihuHtml;
import org.hq.pojo.GroupsVideo;
import org.hq.pojo.HostWeb;
import org.hq.pojo.VideoMessage;
import org.hq.pojo.WebName;
import org.hq.service.sql.MysqlManageService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Set;

import static org.hq.upvideo.RedisKey.PARSE_HTML_SIHU_ERROR_KEY;
import static org.hq.upvideo.RedisKey.UP_VVIDE_MESSAGE_TASK_SAVE_SIHU_KEY;

@SpringBootTest
class UpVideoServiceTest {
    //数据库管理对象
    @Autowired
    private MysqlManageService sqls;
    @Autowired
    UpVideoService ups;
    @Autowired
    private RedisTemplate<String, String> redisString;

    @Test
    void submit() {
        for (int i = 0; i < 100; i++) {
            //boolean b = ups.submit(new UpVideoServiceThreadPool.Task());
            // assertTrue(b);
        }
    }

    @Test
    void saveGroupPageCountTask() {
        String key = String.valueOf(RedisKey.UP_VVIDE_TASK_SAVE_SIHU_KEY);
        //1 获取所以网站对象
        List<WebName> lwn = sqls.getLisetWebName();

        for (WebName wn : lwn) {
            //获取最新网址
            HostWeb hw = sqls.getLimitHostWeb(wn);
            //获取子分组集合
            List<GroupsVideo> gvi = this.sqls.getListGroupsItme(wn);
            for (GroupsVideo gv : gvi) {
                //联网获取网页数量

                // ups.redisSaveList(key,taskdata);
            }

        }


    }

    @Test
    void update() {
        ups.update();
    }

    @Test
    void bark() {
        String key=String.valueOf(UP_VVIDE_MESSAGE_TASK_SAVE_SIHU_KEY);
        List<String> list = this.redisString.opsForList().range(key, 0, -1);
        String bark="UP_VVIDE_MESSAGE_TASK_SAVE_SIHU_KEY_BARK";
        //数据拷贝
        this.redisString.opsForList().rightPushAll(bark, list);


    }
    @Test
    void huifu() {
        String bark="UP_VVIDE_MESSAGE_TASK_SAVE_SIHU_KEY_BARK";
        String key=String.valueOf(UP_VVIDE_MESSAGE_TASK_SAVE_SIHU_KEY);
        List<String> list = this.redisString.opsForList().range(bark, 0, -1);

        //数据拷贝
        this.redisString.opsForList().rightPushAll(key, list);


    }
    @Test
    void UpVideoEntityToUpVideoTaskEntity() {
        String key=String.valueOf(UP_VVIDE_MESSAGE_TASK_SAVE_SIHU_KEY);
        List<String> list = this.redisString.opsForList().range(key, 0, 50);
        //将 json 数据集合 实例化为对象
        List<VideoMessage> vmlist = JSON.parseArray(list.toString(), VideoMessage.class);

      for (VideoMessage vm : vmlist){
          System.out.println(vm.getName());
      }


    }

    @Test
    void parseArray(){
        this.ups.finish();
    }











    @Test
    void finish(){
        this.ups.finish();
    }

    @Test
    void error() {
        SihuHtml sh = new SihuHtml();
        String key=String.valueOf(PARSE_HTML_SIHU_ERROR_KEY);
        Set<String> members = this.redisString.opsForSet().members(key);
        for (String json : members){
            VideoTaskEntity entity = JSON.parseObject(json, VideoTaskEntity.class);
           switch (entity.getTaskType()){
               case PARSE_HTML_TASK_SAVE_SIHU_VIDEO_PLAY_PAGE:
                   String uir =entity.getWebHost()+entity.getTaskPath();
                   sh.siHuVideoPaly(uir);
                   break;
           }





            //System.out.println(entity.getTaskPath());
        }


    }
}